#include "dossier.h"
#include "bdd.h"
#include "options.h"

dossier::dossier(const QString & path)
{
    this->setPath(path);
}

int dossier::Existe()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idDossier FROM dossier WHERE cheminDossier ="
            + db.TexteVersSql(dossier::fromNativeSeparators(this->absolutePath()));
    return db.EstDansLaBase(requete);
}

void dossier::Enregistre()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "INSERT INTO dossier(idDossier, cheminDossier) VALUES(NULL, "
                +db.TexteVersSql(this->absolutePath())+")";
    db.Execute(requete);
}

void dossier::Supprime()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "DELETE FROM dossier WHERE cheminDossier="+db.TexteVersSql(dossier::fromNativeSeparators(this->absolutePath()));
    db.Execute(requete);
}

QList<dossier> dossier::ListeDossiers()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idDossier, cheminDossier FROM dossier ORDER BY cheminDossier";
    db.Execute(requete);

    QList<dossier> listeDossiers;

    while(db.Requete().next())
    {
        dossier unDossier = dossier(dossier::toNativeSeparators(db.Requete().value(1).toString()));
        unDossier.idDossier = db.Requete().value(0).toInt();
        listeDossiers << unDossier;

    }
    return listeDossiers;
}

void dossier::setId(int id)
{
    this->idDossier = id;
}

int dossier::id()
{
    return this->idDossier;
}
